<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字典</title>
</head>
<body>
<script>
    function Dictionary(){
        this.data=[];
    }
    Dictionary.prototype={
        constructor:Dictionary,
        add: function (key,val) {
            this.data[key]=val;
        },
        find: function (key) {
            return this.data[key];
        },
        remove: function (key) {
            delete this.data[key];
        },
        each: function (cb) {
            for(var key in this.data){
                cb(key,this.data[key],this.data);
            }
        },
        show: function () {
            console.info('show:',this.data);
            var keys=Object.keys(this.data).sort();
            for(var k in keys){
                console.log(keys[k],':',this.data[keys[k]]);
            }
        },
        size: function () {
            var n=0;
            this.each(function () {
               ++n;
            });
            return n;
        },
        clear: function () {
            delete this.data;
            this.data=[];
        }
    }

    var d=new Dictionary();
    d.add('a','A');
    d.add('c','C');
    d.add('b','B');
    d.add('e','E');
    d.add('f','F');
    d.add('d','D');
    console.log('find e:', d.find('e'));
    d.remove('e');
    console.log('size:', d.size());
    d.show();


</script>
</body>
</html>